//
//  LWRegisterOneCtrl.m
//  laowu
//
//  Created by 尹桥印 on 15/4/29.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "LWRegisterOneCtrl.h"
#import "LWWeatherCtrl.h"
#import "LWRegisterTwoCtrl.h"
#import "LifeWebViewCtrl.h"

@interface LWRegisterOneCtrl ()<UIScrollViewDelegate>
@property (nonatomic,strong)NSTimer *countDownTimer;
@property (nonatomic,assign)int time;
@end

@implementation LWRegisterOneCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"注册"];
    [self.scrollView setDelegate:self];
    [self.scrollView setAlwaysBounceVertical:YES];
    [self.registerBtn setBackgroundImage:[UIImage imageWithColor:THEME_COLOR] forState:UIControlStateNormal];
    [self.registerBtn setBackgroundImage:[UIImage imageWithColor:RGBColor(179, 179, 179)] forState:UIControlStateDisabled];
    [self.registerBtn addTarget:self action:@selector(registerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.phoneView setCornerRadius:5 borderWidth:1 borderColor:HEXColor(@"dfdfdf")];
    [self.veriCodeView setCornerRadius:5 borderWidth:1 borderColor:HEXColor(@"dfdfdf")];
    
    [self.sendCodeBtn setBackgroundImage:[UIImage imageWithColor:THEME_COLOR] forState:UIControlStateNormal];
    [self.sendCodeBtn setBackgroundImage:[UIImage imageWithColor:RGBColor(179, 179, 179)] forState:UIControlStateDisabled];
    [self.sendCodeBtn addTarget:self action:@selector(sendCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.time = 60;
    [self.readBtn addTarget:self action:@selector(readBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)registerBtnClick:(UIButton *)btn{
    
    if (ISBLANK(self.veriCodeField.text)) {
        [DialogUtils showTipsWithView:nil String:@"请输入验证码"];
        return;
    }
//    LWRegisterTwoCtrl *ctrl = [[LWRegisterTwoCtrl alloc] initWithPhone:self.phoneField.text];
//    [self.navigationController pushViewController:ctrl animated:YES];
    
    MBProgressHUD *hud = [DialogUtils showLoadingWithView:nil];
    [MeDataTool getSucWithPhone:self.phoneField.text code:self.veriCodeField.text success:^(BaseResponse *result) {
        [hud hide:YES];
        if (result.isSucc) {
            LWRegisterTwoCtrl *ctrl = [[LWRegisterTwoCtrl alloc] initWithPhone:self.phoneField.text];
            [self.navigationController pushViewController:ctrl animated:YES];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [hud hide:YES];
        [DialogUtils showTipsWithView:nil String:@"校验验证码失败"];
    }];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

-(void)sendCodeBtnClick:(UIButton *)btn{
    if (ISBLANK(self.phoneField.text)) {
        [DialogUtils showTipsWithView:nil String:@"请输入手机号码"];
        return;
    }
    //发送验证码
    [MeDataTool getVeriCodeWithPhone:self.phoneField.text success:^(BaseResponse *result) {
        if ([result isSucc]) {
            [DialogUtils showTipsWithView:nil String:@"请求验证码成功"];
            [self startCountDown];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTipsWithView:nil String:@"请求验证码失败"];
    }];
}

/**
 开始倒计时的操作
 */
- (void)startCountDown {
    //处理倒计时
    NSString* time_str = [NSString stringWithFormat:@"倒计时%d秒",self.time];
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_countDownTimer forMode:NSDefaultRunLoopMode];
    [self.sendCodeBtn  setEnabled:NO];
    [self.sendCodeBtn  setTitle:time_str forState:UIControlStateDisabled];
}

/**
 *  倒计时调用的方法
 */
-(void)timeFireMethod{
    if(self.time <= 0){
        [_countDownTimer invalidate];
        [self.sendCodeBtn setEnabled:YES];
        [self.sendCodeBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
        self.time = 60;
    }else{
        self.time--;
        NSString *timeStr = [NSString stringWithFormat:@"倒计时%d秒",self.time];
        [self.sendCodeBtn setTitle:timeStr forState:UIControlStateDisabled];
    }
}

-(void)readBtnClick:(UIButton *)btn{
    [self.readBtn setSelected:!self.readBtn.selected];
    [self.registerBtn setEnabled:self.readBtn.selected];
}

- (IBAction)protocolBtnClick:(id)sender {
    LifeWebViewCtrl *ctrl = [LifeWebViewCtrl initWithUrl:@"http://www.goyoton.com/agreement/index.jhtml" title:@"用户协议"];
    [self.navigationController pushViewController:ctrl animated:YES];
}


@end
